package com.cyru.statemachine.core.impl;

import com.cyru.statemachine.core.State;
import com.cyru.statemachine.core.Transition;
import com.cyru.statemachine.core.Trigger;

import java.util.Objects;

/**
 * description:
 *
 * @author zhangxuchuan
 * date 2022/5/1
 */
public class TransitionImpl<S, E, C> implements Transition<S, E, C> {

    private State<S, E, C> source;

    private State<S, E, C> target;

    private Trigger<S, E, C> trigger;

    @Override
    public void setSourceState(State<S, E, C> source) {
        this.source = source;
    }

    @Override
    public State<S, E, C> getSourceState() {
        return source;
    }

    @Override
    public void setTargetState(State<S, E, C> target) {
        this.target = target;
    }

    @Override
    public State<S, E, C> getTargetState() {
        return target;
    }

    @Override
    public void setTrigger(Trigger<S, E, C> trigger) {
        this.trigger = trigger;
    }

    @Override
    public Trigger<S, E, C> getTrigger() {
        return trigger;
    }

    @Override
    public State<S, E, C> execute(C context) {
        if (trigger == null) {
            return source;
        }
        trigger.execute(source.getId(), target.getId(), trigger.getEvent(), context);
        return null;
    }

    @Override
    public boolean check(C context) {
        return false;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        TransitionImpl<?, ?, ?> that = (TransitionImpl<?, ?, ?>) obj;
        return Objects.equals(source, that.source) && Objects.equals(target, that.target) && Objects.equals(trigger, that.trigger);
    }
}
